package com.huc.rabbit.mq.config;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 通过channel实现拉模式
 */
@Configuration
public class ChannelConfig {

    public static final String EXCHANGE_NAME = "channel.exchange";

    public static final String QUEUE_NAME = "channel.queue";

    public static final String CONSUMER_ROUTING_KEY = "channel.#";

    public static final String PROVIDER_ROUTING_KEY = "channel.key";

    @Bean
    public Channel channelProvider(){
        ConnectionFactory factory = new ConnectionFactory();
        factory.setPassword("admin");
        factory.setUsername("admin");
        factory.setPort(5672);
        factory.setHost("192.168.31.200");
        try{
            Connection connection = factory.newConnection();
            return connection.createChannel();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Bean
    public Channel channelConsumer(){
        ConnectionFactory factory = new ConnectionFactory();
        factory.setPassword("admin");
        factory.setUsername("admin");
        factory.setPort(5672);
        factory.setHost("192.168.31.200");
        try{
            Channel channel = factory.newConnection().createChannel();
            channel.exchangeDeclare(EXCHANGE_NAME,"topic",true,false,null);
            channel.queueDeclare(QUEUE_NAME,true,false,false,null);
            channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,CONSUMER_ROUTING_KEY);
            return channel;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }





}
